package com.eight.cloud.client.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 对象转换
 *
 * @ClassName: ObjectFormat
 * @Author: TXC
 * @Date: 2024-10-08 17:16
 **/
public class ObjectFormat {
    private final static ObjectMapper mapper = new ObjectMapper();

    /**
     * 对象转字符串
     *
     * @param object 对象
     * @return 字符串
     */
    public static String objectToStr(Object object) {
        try {
            return object == null ? null : mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 对象转字节
     *
     * @param object 对象
     * @return 字节
     */
    public static byte[] objectToByte(Object object) {
        try {
            return object == null ? null : mapper.writeValueAsBytes(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 字节转对象
     *
     * @param object 字节
     * @return 对象
     */
    public static <T> T byteToObj(Class<T> clazz, byte... object) {
        try {
            return object == null ? null : mapper.readValue(object, clazz);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * json 转对象
     *
     * @param str   json字符串
     * @param clazz 对象类型
     * @return 对象
     */
    public static <T> T strToObject(String str, Class<T> clazz) {
        try {
            return str == null ? null : mapper.readValue(str, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 对象转对象
     *
     * @param object 对象
     * @param clazz  对象类型
     * @return 对象
     */
    public static <T> T objToObject(Object object, Class<T> clazz) {
        if (object instanceof String) {
            return strToObject((String) object, clazz);
        }
        return strToObject(objectToStr(object), clazz);
    }

    /**
     * 从json中提取指定字段
     *
     * @param responseFields 字段集合 key:字段名 value:json路径 路径示例：/data/name
     * @param response       json字符串
     * @return
     * @throws IOException
     */
    public static Map<String, Object> parseResponse(Map<String, String> responseFields, String response) {
        try {
            JsonNode rootNode = mapper.readTree(response);
            Map<String, Object> parsedData = new HashMap<>();
            for (Map.Entry<String, String> entry : responseFields.entrySet()) {
                JsonNode fieldNode = rootNode.at(entry.getValue());
                if (fieldNode != null && !fieldNode.isNull()) {
                    parsedData.put(entry.getKey(), mapper.writeValueAsString(fieldNode));
                }
            }
            return parsedData;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
